<?php
class Jabatan_model extends MY_Model {
	
    function Jabatan_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "jabatan";
        $this->table_fields = array("jbt_id","jbt_nama","jbt_supervisi","dep_id", "dep_default");
    }
    
	/**
     * return jabatan default untuk departemen spesifik
     * @param $dep_id
     */
    function getDefaultJabatanFor($dep_id){
    	$res = $this->get_where("dep_id='$dep_id' AND dep_default=1");
    	if ($res = $res->row())	return $res->jbt_id;
    	return false;
    }
    
    function select_all($orderby=false){
    	if (is_array($orderby)){
			foreach($orderby as $kk=>$vv){
				$this->order_by($kk, $vv);
			}
		}
		else if ($orderby){
			$this->order_by($orderby);
		}
    	$this->join('departemen','departemen.dep_id='.$this->table_name.'.dep_id','left');
    	return $this->get();
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    	if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "").$this->table_name.".$ff LIKE '%$kk%'"; 
				}
				
				//join table
				foreach(array('dep_nama') as $ff){
					$qq .= ($qq? " OR " : "")."$ff LIKE '%$kk%'"; 
				}
			}
			$this->where("($qq)");
		}
		if ($where) $this->where($where);
		if ($orderby) $this->order_by($orderby);
		
		$this->join('departemen as c',"c.dep_id=$this->table_name.dep_id",'left');
		return $this->get($this->table_name,$limit, $offset); 
    }
    
    function select_by_dep($depID, $orderby=false){
    	if ($orderby) $this->order_by($orderby);
		$this->join('departemen', 'departemen.dep_id = '.$this->table_name.'.dep_id');
    	return $query = $this->get_where(array($this->table_name.'.dep_id' => $depID));
    }
        
    function select_by_id($id, $orderby=false){
    	if ($orderby) $this->db->order_by($orderby);
    	$this->db->select('*');
		$this->db->join('departemen', 'departemen.dep_id = '.$this->table_name.'.dep_id');
    	return $query = $this->db->get_where($this->table_name, array('jbt_id' => $id));
    }

	function getSupervisor($jbt_id){
		$data = $this->select_by_id($jbt_id);
		$data = $data->row();
		return $data->jbt_supervisi;
	}
}